﻿using EMRView;
using EMRView.Extensions.HCAttribute;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EMRView.Extensions.Command.StandardCommand.File
{
    [Command(CommandType.文件, "SaveFile", "保存文件，仅用于文件形式保存,该命令会返回保存文件的名称")]
    public class SaveFileCommand : BaseCommand
    {
        public override Icon CommandIcon
        {
            get
            {
                return Properties.HCResource.保存;
            }
        }
        public override object Execute(HCEmrView _view, bool showDefaultUI = false, params object[] para)
        {
            string fileName = "";
            if (_view.FileName != "")
            {
                fileName = _view.FileName;
                _view.SaveToFile(_view.FileName);
            }
            else
            {
                SaveFileDialog vDlg = new SaveFileDialog();
                try
                {
                    vDlg.Filter = "文件|*" + HC.View.HC.HC_EXT;
                    if (vDlg.ShowDialog() == DialogResult.OK)
                    {
                        if (vDlg.FileName != "")
                        {
                            if (System.IO.Path.GetExtension(vDlg.FileName) != HC.View.HC.HC_EXT)
                                vDlg.FileName += HC.View.HC.HC_EXT;


                            fileName = vDlg.FileName;
                            _view.SaveToFile(vDlg.FileName);
                            _view.IsChanged = false;
                        }
                    }
                }
                finally
                {
                    vDlg.Dispose();
                    GC.Collect();
                }
            }
            return fileName;
        }

        public override void InitControl(object control)
        {
        }
    }
}
